﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicareModel;
using MedicareBusiness;

namespace MedicareWebUI
{
    public partial class PatientManagement : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            btnAdd.Enabled = false; 
            txtID.Enabled = false;
            this.disableValidations();
            txtSearchDate.Visible = false; 

            gridResults.Visible = false; 
        }

        private void updateFields(int id)
        {
            PatientBO patients = new PatientBO();
            Patient aPatient = patients.getPatientByID(id);
            txtID.Text = Convert.ToString(id); 
            txtName.Text = aPatient.name;
            txtLastName.Text = aPatient.lastName;
            txtSSN.Text = aPatient.ssn;
            txtDOB.Text = aPatient.dob.ToString("yyyy-MM-dd");
            txtAddress.Text = aPatient.address;
            txtPhone.Text = aPatient.phone;
            this.enableValidations(); 
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Patient aPatient = new Patient();
            aPatient.name = txtName.Text;
            aPatient.lastName = txtLastName.Text;
            aPatient.ssn = txtSSN.Text;
            aPatient.dob = Convert.ToDateTime(txtDOB.Text);
            aPatient.address = txtAddress.Text;
            aPatient.phone = txtPhone.Text;

            PatientBO patients = new PatientBO();

            patients.addPatient(aPatient);
            gridPatients.DataBind();

            this.enableValidations();
            btnAdd.Enabled = false;
            btnEvent.Enabled = true;
            btnEMT.Enabled = true;
        }

        protected void gridPatients_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int)gridPatients.SelectedDataKey.Value;
            updateFields(id);
            btnAdd.Enabled = false;
            btnUpdate.Enabled = true;
            btnEvent.Enabled = false;
            btnEMT.Enabled = false; 
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Patient aPatient = new Patient();
                aPatient.idPatient = Convert.ToInt16(txtID.Text);
                aPatient.name = txtName.Text;
                aPatient.lastName = txtLastName.Text;
                aPatient.ssn = txtSSN.Text;
                aPatient.dob = Convert.ToDateTime(txtDOB.Text);
                aPatient.address = txtAddress.Text;
                aPatient.phone = txtPhone.Text;

                PatientBO patients = new PatientBO();
                patients.updatePatient(aPatient);
                gridPatients.DataBind();

                this.enableValidations();
                btnAdd.Enabled = false;
                btnEvent.Enabled = true;
                btnEMT.Enabled = true;
            }

            else
            {
            }
        }

        protected void btnEvent_Click(object sender, EventArgs e)
        {
            Response.Redirect("EventManagement.aspx");
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = true;
            txtID.Enabled = false;
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false; 
            btnEvent.Enabled = false;
            btnEMT.Enabled = false;
            this.enableValidations(); 
        }

        private void disableValidations()
        {
            ValName.Enabled = false;
            ValLastName.Enabled = false;
            ValSSN.Enabled = false;
            ValDOB.Enabled = false; 
        }

        private void enableValidations()
        {
            ValName.Enabled = true;
            ValLastName.Enabled = true;
            ValSSN.Enabled = true;
            ValDOB.Enabled = true;
        }

        protected void btnEMT_Click(object sender, EventArgs e)
        {
            Response.Redirect("EMTManagement.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "Date of Birth")
            {
                TextBox1.Text = txtSearchDate.Text;
                gridPatients.Visible = false;
                gridResults.Visible = true;
                txtSearchDate.Visible = true;
                gridResults.DataBind();
            }

            else
            {
                gridPatients.Visible = false;
                gridResults.Visible = true;
                gridResults.DataBind();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            gridPatients.Visible = true;
            gridResults.Visible = false;
            txtSearchDate.Visible = false;
            TextBox1.Visible = true;
            TextBox1.Text = ""; 
            DropDownList1.SelectedIndex = 0; 
        }

        protected void ValDOB_ServerValidate(object source, ServerValidateEventArgs args)
        {
            try
            {
                DateTime.ParseExact(args.Value, "yyyy-mm-dd",
                    System.Globalization.DateTimeFormatInfo.InvariantInfo);
                args.IsValid = true;
            }
            catch
            {
                args.IsValid = false;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "Date of Birth")
            {
                TextBox1.Visible = false;
                txtSearchDate.Visible = true;
            }

            else
            {
                TextBox1.Visible = true;
                txtSearchDate.Visible = false;
            }

        }

        protected void gridResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int)gridResults.SelectedDataKey.Value;
            updateFields(id);
            btnAdd.Enabled = false;
            btnUpdate.Enabled = true;
            btnEvent.Enabled = false;
            btnEMT.Enabled = false;
            gridPatients.Visible = false;
            gridResults.Visible = true; 
        }
    }
}